<?php


namespace app\crm\logic;


use app\shop\model\SampleApply;
use think\facade\Db;

class SampleLogic extends AbstractLogic
{

    /**
     * 获取数据条件
     * @param $request
     * @author Mr.Li
     * @create 2021-08-12 14:22
     */
    protected function getMessageWhere($request)
    {
        $map = [
            ['is_audit','=',1]
        ];
        return $map;
    }

    /**
     * 获取消息数量
     * @param $userId
     * @param array $request
     * @return int
     * @author Mr.Li
     * @create 2021-08-10 11:09
     */
    public function getMessageNum($userId,array $request): int
    {
        $count = 0;
        $map = $this->getMessageWhere($request);
        try {
            // 获取数据权限UID范围
            $dataAuthUid = (new \app\admin\model\User())->getDataAuthUid($userId, 'sample');
            $count = SampleApply::where($map)
                ->when(!in_array('all', $dataAuthUid), function($query) use ($dataAuthUid) {
                    // 数据权限
                    $query->where('apply_user_id', 'in', $dataAuthUid);
                })
                ->count();
        }catch (\Exception $e) {
        }
        return $count;
    }


    /**
     * 获取审批中试样申请列表
     * @param $userId
     * @param array $request
     * @return int
     * @author Mr.Li
     * @create 2021-08-12 11:50
     */
    public function getMessageList($userId,array $request): array
    {
        $data              = [];
        $map = $this->getMessageWhere($request);
        $dataAuthUid = (new \app\admin\model\User())->getDataAuthUid($userId, 'sample');
        $sqlQuery = SampleApply::alias('a')->join('shop_goods g', 'g.id = a.goods_id', 'LEFT')
            ->where($map)
            ->when(!in_array('all', $dataAuthUid), function($query) use ($dataAuthUid) {
                // 数据权限
                $query->where(function($query) use ($dataAuthUid) {
                    $query->where('a.apply_user_id', 'in', $dataAuthUid);
                });
            });
        $dataCount = $sqlQuery->count();
        if ($dataCount) {
            $field = ['a.id', 'a.goods_id', 'a.quantity', 'a.apply_user_id', 'a.is_audit',
                'a.create_time', 'a.apply_reason', 'g.name', 'g.is_standard_product'];
            $list  = $sqlQuery->field($field)
                ->limit(($request['page'] - 1) * $request['limit'], $request['limit'])
                ->order('a.id DESC')
                ->select()->toArray();
            if ($list) {
                // 商品信息
                $goodsIdArr = array_unique(array_column($list, 'goods_id'));
                $where      = [
                    ['g.goods_id', 'in', $goodsIdArr]
                ];
                $field    = ['u.big_unit_name', 'u.small_unit_name', 'g.item_id', 'g.sample_unit'];
                $unitList = Db::name('shop_goods_sku')->alias('g')
                    ->join('shop_goods_unit u', 'g.unit_id = u.unit_id', 'LEFT')
                    ->where($where)->column($field, 'g.goods_id');

                // 商品规格
                $specIdArr = array_unique(array_column($unitList, 'item_id'));
                $specWhere = [
                    ['item_id', 'in', $specIdArr]
                ];
                $specList = Db::name('shop_goods_spec')->where($specWhere)->column('name', 'item_id');

                $userIdArr = array_unique(array_column($list, 'apply_user_id'));
                $userList  = Db::name('admin_user')->where('id', 'in', $userIdArr)->column('realname', 'id');
                $auditArr  = ['1' => '申请中', '2' => '已通过', '3' => '未通过'];
                foreach ($list as $key => $val) {
                    $unitInfo  = $unitList[$val['goods_id']] ?? [];
                    $itemId    = $unitInfo['item_id'] ?? 0;
                    $list[$key]['sample_unit']     = $unitInfo['sample_unit'] ?? '';
                    $list[$key]['big_unit_name']   = $unitInfo['big_unit_name'] ?? '';
                    $list[$key]['small_unit_name'] = $unitInfo['small_unit_name'] ?? '';
                    $list[$key]['spec_name']       = $specList[$itemId] ?? '';
                    $list[$key]['apply_user_name'] = $userList[$val['apply_user_id']] ?? '';
                    $list[$key]['is_audit_name']   = $auditArr[$val['is_audit']] ?? '';
                    $list[$key]['is_standard_product_name'] = $val['is_standard_product'] == 1 ? '标' : '定';
                }
            }
        }
        $permission = get_user_permission_menu($userId,'foretaste_list_tag');
        $data['menu_list'] = $permission ? $permission : [];
        $data['list']      = $list ?? [];
        $data['dataCount'] = $dataCount ?? 0;
        return $data;

    }




}
